package com.hp.utils.excel.strategy.impl;


import com.hp.enums.ExcelCellTypeEnum;
import com.hp.utils.excel.ExcelConstraintResolve;
import com.hp.utils.excel.strategy.ConstraintTypeStrategy;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class BasicConstraintStrategyFactory {

    /**
     * 策略集合
     */
    static final Map<String, ConstraintTypeStrategy> CELL_TYPE_STRATEGY_MAP = new ConcurrentHashMap<>();

    /**
     * 暴露一个getStrategy方法，用于获取策略
     */
    public static ConstraintTypeStrategy getStrategy(String type) {
        return CELL_TYPE_STRATEGY_MAP.get(type);
    }


    /**
     * 公共方法：设置单元格验证
     */
    public void setCellValidation(ExcelConstraintResolve resolve,
                                  DataValidationHelper helper,
                                  DataValidationConstraint constraint,
                                  Sheet sheet,
                                  Boolean isShowPromptBox,
                                  Boolean isShowErrorBox) {
        // 提示信息
        String desc = ExcelCellTypeEnum.getDescByType(resolve.getType());
        // 设置下拉列表的行： 首行，末行，首列，末列
        CellRangeAddressList rangeList = new CellRangeAddressList(resolve.getFirstRow(), resolve.getLastRow(), resolve.getIndex(), resolve.getIndex());
        // 设置约束
        DataValidation validation = helper.createValidation(constraint, rangeList);
        // 选中单元格时的提示信息
        validation.setShowPromptBox(isShowPromptBox);
        validation.createPromptBox(resolve.getTitle(), desc);
        // 输入不合规数据时弹框
        validation.setShowErrorBox(isShowErrorBox);
        validation.setErrorStyle(DataValidation.ErrorStyle.STOP);
        validation.createErrorBox("ERROR", desc);
        sheet.addValidationData(validation);
    }


}

